<?php
/**
 * Create Video and Music widget
 * User: Long
 * Date: 3/23/11
 * Time: 1:39 AM
 */
 
class WVMusicVideoWidget {
    var $plugin_name = "wv-votd";
	
    function WVMusicVideoWidget() {
		add_action( 'init', array(&$this, 'register_custom_widget') );
    }

    function register_custom_widget() {
		$options = get_option('wv_widget');
//        delete_option('wv_widget');
        if (!$options['video_title']) { //Default value
            $options['video_title'] = __("Video of the Day", "wv");
            $options['video_width'] = 185;
            $options['video_height'] = 150;
            update_option('wv_widget', $options);
        }

        if (!$options['song_title']) { //Default value
            $options['song_title'] = __("Song of the Day", "wv");
            $options['song_width'] = 185;
            $options['song_height'] = 150;
            update_option('wv_widget', $options);
        }

		$this->video_title = $options['video_title'];
		$this->song_title = $options['song_title'];
	
        wp_register_sidebar_widget($this->plugin_name.'-videowidget', $this->video_title, array(&$this, 'init_video_widget'), array('description' => __('Add video of the day to sidebar panel', "wv")));
        wp_register_widget_control($this->plugin_name.'-videowidget', $this->video_title, array(&$this, 'init_video_widget_control'));
        wp_register_sidebar_widget($this->plugin_name.'-musicwidget', $this->song_title, array(&$this, 'init_song_widget'), array('description' => __('Add song of the day to sidebar panel', "wv")));
        wp_register_widget_control($this->plugin_name.'-musicwidget', $this->song_title, array(&$this, 'init_song_widget_control'));

    }

    /**
     * Render widget layout
     * @return void
     */
    function init_video_widget($args) {
        global $wpdb, $VipersVideoQuicktags;
        extract($args);
        $options = get_option('wv_widget');
        $title = $options['video_title'];
		$current_year = date('Y');
		$current_month = date('m');
		$current_day = date('j');

		add_filter('the_content', array(&$this, 'disable_widget_video'), 1989);

        echo $before_widget . $before_title . $title . $after_title;
        $widget_query = new WP_Query("post_type=video&posts_per_page=1&year=$current_year&monthnum=$current_month&day=$current_day");
        include('views/show-video-widget.php');
        echo $after_widget;

		remove_filter('the_content', array(&$this, 'disable_widget_video'), 1989);
    }

    /**
     * Create widget control
     * @return void
     */
    function init_video_widget_control() {
        $options = get_option('wv_widget');

        if ($_POST['wv_widget_submit']) {
            $options['video_title'] = strip_tags(stripslashes($_POST['wv_widget_title']));
//            $options['video_width'] = strip_tags(stripslashes($_POST['wv_widget_width']));
//            $options['video_height'] = strip_tags(stripslashes($_POST['wv_widget_height']));
            update_option('wv_widget', $options);
        }
        $title = $options['video_title'];
//        $width = $options['video_width'];
//        $height = $options['video_height'];

        include('views/video-widget-control.php');
    }

    /**
     * Render widget layout
     * @return void
     */
    function init_song_widget($args) {
        global $wpdb;
        extract($args);
        
        $options = get_option('wv_widget');
        $title = $options['song_title'];

		$current_year = date('Y', current_time('timestamp'));
		$current_month = date('m', current_time('timestamp'));
		$current_day = date('j', current_time('timestamp'));

        echo $before_widget . $before_title . $title . $after_title;
		$widget_query = new WP_Query("post_type=music&posts_per_page=1&year=$current_year&monthnum=$current_month&day=$current_day");
        include('views/show-music-widget.php');
        echo $after_widget;
    }

    /**
     * Create widget control
     * @return void
     */
    function init_song_widget_control() {
        $options = get_option('wv_widget');

        if ($_POST['wv_widget_submit']) {
            $options['song_title'] = strip_tags(stripslashes($_POST['wv_widget_title']));
//            $options['song_width'] = strip_tags(stripslashes($_POST['wv_widget_width']));
//            $options['song_height'] = strip_tags(stripslashes($_POST['wv_widget_height']));
            update_option('wv_widget', $options);
        }
        $title = $options['song_title'];
//        $width = $options['song_width'];
//        $height = $options['song_height'];

        include('views/music-widget-control.php');
    }

	function disable_widget_video($html)
	{
		$html = str_replace('id="vvq', 'id="disable_vvq', $html);
		return $html;
	}
}
?>